﻿/******************************************************************
 *Company: http://www.xiaomutech.com/
 *fileName : alarmclock.cpp --- AlarmClock
 *Auth       : yhni (QQ:393320854)
 *Create    : 2022/2/9
 *Description   :
 *Histroy:
 *<Auth>    <Date>        <Ver>        <Content>
 *         2022/2/9
 *******************************************************************/
#include "alarmclock.h"

#include <QPainter>

AlarmClock::AlarmClock(QWidget *parent) : QWidget(parent)
{
    m_timeTick = 30;
    m_timer = new QTimer(this);
    m_timer->setInterval(1000);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(sltTimeTick()));
}

AlarmClock::~AlarmClock()
{
    stop();
}

void AlarmClock::start(int count)
{
    stop();
    m_timeTick = count;
    m_timer->start();
    this->show();
}

void AlarmClock::stop()
{
    if (m_timer->isActive()) m_timer->stop();
}

void AlarmClock::sltTimeTick()
{
    m_timeTick--;
    if (m_timeTick <= 0) {
        m_timer->stop();
        m_timeTick = 0;
        emit timeout();
    }

    this->update();
}

QSize AlarmClock::sizeHint() const
{
    return QSize(90, 96);
}

void AlarmClock::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.scale(this->width() / 90.0, this->height() / 96.0);
    QRect rect(0, 0, 90, 96);
    painter.drawPixmap(rect, QPixmap(":/images/clock.png"));
    painter.setPen(QColor("#000000"));
    QFont font = painter.font();
    font.setBold(true);
    font.setPixelSize(30);
    painter.setFont(font);
    painter.drawText(rect, Qt::AlignCenter, QString::number(m_timeTick));
}
